
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import api from '../../utils/api'
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    active: {
      type: Number,
      value: 0
    },
    list: {
      type:Array,
      value:[
        {
          text: "首页",
          normal: '/images/tabbar/index.png',
          active: '/images/tabbar/index-green.png'
        },
        {
          text: "发现",
          normal: '/images/tabbar/faxian.png',
          active: '/images/tabbar/faxian-green.png'
        },
        {
          text: "资讯",
          normal: '/images/tabbar/zixun.png',
          active: '/images/tabbar/zixun-green.png'
        },
        {
          text: "我的",
          normal: '/images/tabbar/my.png',
          active: '/images/tabbar/my-green.png'
        }
      ]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    logincode:'',
    authres:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(e) {
      const userType = getApp().globalData.userType
      const index = e.detail
      let url = ''
      if (index === 0) {
        url = '/pages/index/index'
      } else if (index === 1) {
        url = '/pages/find/index'
      } else if (index === 2) {
        url = '/pages/news/index'
      } else if (index === 3) {   
        url = '/pages/my/index'
      }
      if( index<3 )
      wx.switchTab({
        url,
      })
      else{
        if(!api.isauth()){
          wx.navigateTo({
            url: '/pages/login/index'
          })
        }else{
          wx.switchTab({
            url: '/pages/my/index'
          })
        }
      }
    },
    async auth(){
      let _this = this
      if(!app.globalData.userInfo || !app.globalData.userInfo.name){
        wx.login({
          success: function (res) {
            _this.setData({logincode:res.code})
          }
        })
        wx.getUserProfile({
          desc: '用于完善会员资料', 
          success: (res) => {
            //console.log(res)
            _this.setData({authres:res})
            _this.send()
          }
        })
      }else{
        wx.reLaunch({
          url: '/pages/my/index'
        })
      }
    },

    async send() {
      let data={}
      data['flag'] = 'get_auth'
      data['code'] = this.data.logincode
      data['data'] = JSON.stringify(this.data.authres)
      //console.log(JSON.stringify(data))
      const {recode, result, message} = await api.send(data)
      if(recode === 'SUCC') {
        console.log(result)
        for(let p in result)
        app.globalData.userInfo[p] = result[p]
        app.globalData.userInfo.name = result.nickName
        app.globalData.userInfo.avatar = result.avatarUrl
  
        wx.reLaunch({
          url: '/pages/my/index'
        })
      }else {
        Toast(message)
      }
  
    },

    bindGetUserInfo(e) {
      const { userInfo } = e.detail
      if(!userInfo) {
        Toast('请授权后使用')
      }else {
        for(let k in userInfo) {
          app.globalData.userInfo[k] = userInfo[k]
        }
        wx.reLaunch({
          url: '/pages/my/index',
        })
      }
    }
  }
})